package com.lindroy.androidplayer.ui.main.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.App
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.databinding.FragmentMineBinding
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.collectWhenResumed
import com.lindroy.androidplayer.utils.kvLoginInfo
import com.lindroy.androidplayer.viewmodels.MineViewModel
import com.lindroy.lib_utils.statics.IntentUtil

/**
 * @author Lin
 * @date 2022/1/10
 * @function 首页-我的
 * @Description
 */
class MineFragment : BaseLazyFragment<FragmentMineBinding>() {

    private val viewModel by viewModels<MineViewModel>()

    override fun FragmentMineBinding.initView(savedInstanceState: Bundle?) {
        vm = viewModel
        viewModel.setLoginInfo(kvLoginInfo)
    }

    override fun FragmentMineBinding.initListener() {
        viewModel.loginClickLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_login)
        }
        viewModel.coinItemClickLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_my_coins)
        }
        viewModel.collectionClickLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_my_collection)
        }
        viewModel.shareItemClickLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_my_share)
        }
        viewModel.settingClickLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_settings)
        }
        viewModel.urlItemClickLiveData.observe(viewLifecycleOwner) {
            WebViewFragment.start(
                this@MineFragment,
                url = it,
                title = getString(R.string.mine_repository_url)
            )
        }
        viewModel.feedbackItemClickLiveData.observe(viewLifecycleOwner){
            IntentUtil.sendEmail(requireContext(), address = "1603683935@qq.com")
        }
        App.eventViewModel.loginInfoFlow.collectWhenResumed(viewLifecycleOwner){
            viewModel.setLoginInfo(it)
        }
    }

    override fun lazyLoad() {
    }

    companion object{
        fun newInstance() = MineFragment()
    }
}